package com.doit.day03

import scala.io.Source



object _02_scala迭代器的测试{
  def main(args: Array[String]): Unit = {
    val iterator: ScalaIterator = new ScalaIterator
    while (iterator.hasNext){
      println(iterator.next())
    }

    println("==========上面测试的是iterator===================")

    val iterable: ScalaIterable = new ScalaIterable
    for (elem <- iterable) {
      println(elem)
    }

    println("==========上面测试的是iterable的增强for循环===================")


    val iterator1: Iterator[Hero] = iterable.iterator
    while (iterator1.hasNext){
      println(iterator1.next())
    }

    println("==========上面测试的是iterable里面的迭代器===================")



  }
}

//在scala里面，也是 单继承，多实现   extends
class ScalaIterator extends Iterator[Hero]{

  val lines: Iterator[String] = Source.fromFile("data/Hero.txt").getLines()

  override def hasNext: Boolean = lines.hasNext

  override def next(): Hero = {
    val line: String = lines.next()
    val arr: Array[String] = line.split(",")
    val arr1: Array[String] = arr(1).split("_")
    val arr2: Array[String] = arr1(1).split("::")
    Hero(arr(0).toInt,arr1(0),arr2(0).toInt,arr2(1).toDouble)
  }
}


class ScalaIterable extends Iterable[Hero]{
  override def iterator: Iterator[Hero] = new ScalaIterator
}



case class Hero(id:Int,name:String,faceValue:Int,power:Double)
